%   gen_load_mask_view
%   Christopher J. Whalen
%   University of Illinois
%   12/08/05 - Date Created
%   12/08/05 - Last Modified

% KL 01-04-11:  changed line 85 to run dos delete command on James

%   Purpose - generate the mri mask or load the mask and view the rendered
%   image. the subroutine loops through changing the thresholds until a
%   desired one is obtained.

function [choice,file,dir,vol_mask,vol,xyz_mask,thresh] =...
          gen_load_mask_view(choice,file,dir,mri_dim,vol,tai)

global vol_mask

% Attempt to load scalp file
%
fid=fopen(file.scalp_zip);
if fid > 1 % scalp file exists - load it
    thresh = []; % Arbitrarily Setting To Zero So No Errors Later
    fprintf('\nLoading Zipped Scalp File %s\n',file.scalp_zip);
    unzip(file.scalp_zip,dir.S);
    load(file.scalp);
    delete(file.scalp);
% scatter3(xyz_mask(:,1),xyz_mask(:,2),xyz_mask(:,3))   
% axis equal
% axis manual
% hold on
% disp('displaying loaded xyz_scalp')

    % View scalp if desired
    %
    choice.view_mri = input('\nView scalp? (y/n) ', 's');
    if strcmpi(choice.view_mri, 'y')==1
        choice.redo_scalp=view_scalp(vol_mask, thresh);
    else
        choice.redo_scalp='n'; % if you don't look you can't change :)
    end
else % if no scalp file
    choice.redo_scalp = 'y'; % assume scalp estimation
end

choice.scalp_redone='n';

while strcmpi(choice.redo_scalp,'y') % keep trying until good threshold found
    
    fprintf('\nEnter up to 5 thresholds, range .02 to .2,\n');
    thresh = input('multiple values in []s, default is 0.05: ');
    if isempty(thresh) 
        thresh = 0.05;  % set default threshold
        disp(['Threshold set to ' num2str(thresh)]);
    end

    vol_mask_cell = cell(length(thresh),1);
    xyz_mask_cell = cell(length(thresh),1);

    % generate and plot mudmen for all desired thresholds
    %
    fprintf('\n');
    for i=1:length(thresh)
        fprintf('Processing Threshold %d/%d \n', i, length(thresh));
%         keyboard
        [xyz_mask, vol_mask, vol] = brk2msk(file,mri_dim,vol,thresh(i));
        vol_mask_cell{i} = vol_mask;
        xyz_mask_cell{i} = xyz_mask;
        %roughly fitting subplots to page
        if length(thresh) >= 7
            subplot(3,3,i); 
        elseif length(thresh) >= 5 & length(thresh) <= 6 
            subplot(2,3,i);
        elseif length(thresh) >= 3 & length(thresh) <= 4 
            subplot(2,2,i);
        elseif length(thresh) == 2  
            subplot(1,2,i);
        elseif length(thresh) == 1
            subplot(1,1,i);
        end
        view_acpc(vol_mask, thresh(i)); % MUDMAN. View MRI and Optode Locations (Not a choice: Must view MRI if no)
        hold on;
        rotate3d on;
        axis off
    end % multiple threshold loop

    % choose desired threshold (or none)
    %
    thresh_pref = input('\nEnter Desired Threshold (or Press Enter if all undesirable): ');
    
    if ~isempty(thresh_pref)
        thresh_num = find(thresh_pref == thresh);
        vol_mask = vol_mask_cell{thresh_num};
        xyz_mask = xyz_mask_cell{thresh_num};
        choice.view_mri='y';
        choice.redo_scalp='n';  %added this terminate the while loop
        choice.scalp_redone='y';
    else
        close; %Close figure window and try again
    end

%     clear xyz_mask_cell vol_mask_cell

    % place into coords w/ (LPA-RPA)/2 origin
    %
    xyz_mask = xyz_mask - repmat(tai.origin,size(xyz_mask,1),1);
    
end % while choice.redo_scalp == y

if strcmpi(choice.scalp_redone,'y') % if we restimated the scalp
    
    % View scalp if desired
    %
    choice.view_mri = input('\nView scalp? (y/n) ', 's');
    if strcmpi(choice.view_mri, 'y')==1
        close all % close multiple thresholded scalp window
        choice.redo_scalp=view_scalp(vol_mask, thresh);
    end
    
    % Save Scalp if desired
    %  in the future must save as brik format and load as brik file !!!
    %
    choice.save_scalp = input('\nSave scalp? (y/n) ', 's');
    if strcmpi(choice.save_scalp,'y') % 
    %     save_scalp(file,choice,vol_mask,xyz_scalp,thresh); % save it
    fprintf('Saving Scalp  %s ...\n',file.scalp);
        save(file.scalp, 'vol_mask', 'xyz_mask','tai');
        zip(file.scalp_zip,file.scalp);
        delete file.scalp
    end
end

function choice=view_scalp(vol_mask, thresh)

% create the figure window
% 'ScreenSize' = [left, bottom, width, height]
% 'Position' = [left, bottom, width, height]
%
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)/2 scrsz(4)/2-scrsz(4)/4 scrsz(3)/2.2 scrsz(4)/2],...
    'color','w')
view_acpc(vol_mask, thresh);  % MUDMAN           
hold on;
rotate3d on;

choice = input('\nRedo the scalp estimation? (y/n): ','s');
